package com.wanshi.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanshi.entity.Goods;
import com.wanshi.service.IGoodsService;
import com.wanshi.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-16
 */
@CrossOrigin
@RestController
@RequestMapping("/goods")
@Api(tags = "测试GoodsController接口文档")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;


    @ApiOperation(value = "商品分页带条件查询")
    @PostMapping("/findPageAndCount/{ye}/{size}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ye",value = "页码",paramType = "path",dataType = "int"),
            @ApiImplicitParam(name = "size",value = "条数",paramType = "path",dataType = "int"),
    })
    public Result findByPageAndCond(@PathVariable int ye, @PathVariable int size, @RequestBody Goods goods){
        IPage page=new Page(ye,size);
        page = goodsService.findPageGoods(page, goods);
        return Result.ok(page);
    }

    @ApiOperation(value = "添加商品")
    @PostMapping
    public Result addGoods(@RequestBody Goods goods) {
        boolean b = goodsService.save(goods);
        if(b){
            return Result.ok();
        }
        return Result.error("添加供应商失败");
    }


    @ApiOperation(value = "根据id删除商品")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Integer.class, required = true)
            }
    )
    @DeleteMapping("/{id}")
    public Result deleteGoods(@PathVariable String id){
        boolean b = goodsService.removeById(id);
        if(b){
            return Result.ok();
        }
        return Result.error("删除商品失败");
    }

    @ApiOperation(value = "根据id查找商品")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Integer.class, required = true)
            }
    )
    @GetMapping("/{id}")
    public Result findByIdSup(@PathVariable String id) {
        return Result.ok(goodsService.findById(Integer.valueOf(id)));
    }


    @ApiOperation(value = "修改商品")
    @PutMapping("UpdateGoods")
    public Result UpdateGoods(@RequestBody Goods goods) {
        boolean b = goodsService.updateById(goods);
        if(b){
            return Result.ok();
        }
        return Result.error("修改商品失败");
    }

    @ApiOperation(value = "添加或者修改")
    @PostMapping("/goods")
    public Result goods(@RequestBody Goods goods) {
        boolean b = goodsService.saveOrUpdate(goods);
        if(b){
            return Result.ok();
        }
        return Result.error("修改商品失败");
    }

}

